package com.spzx.product.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.utils.ServletUtils;
import com.spzx.common.core.utils.poi.ExcelUtil;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.vo.CategoryVo;
import com.spzx.product.service.ICategoryService;
import com.spzx.system.api.domain.SysUser;
import com.spzx.system.api.model.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.product.domain.Category;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author dongdongxiaoyuanmao
 * @since 2025-03-06
 */
@Tag(name = "商品分类接口管理")
@RestController
@RequestMapping("/category")
public class CategoryController extends BaseController {

    @Autowired
    private ICategoryService categoryService;

    @Operation(summary = "获取分类树")
    @GetMapping("/treeSelect/{id}")
    public AjaxResult treeSelect(@PathVariable("id") Long id) {
        List<Category> list = categoryService.treeSelect(id);
        return success(list);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Category> list = categoryService.list();
        ExcelUtil<Category> util = new ExcelUtil<>(Category.class);
        util.exportExcel(response, list, "商品类别");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<Category> util = new ExcelUtil<>(Category.class);
        util.importTemplateExcel(response, "商品类别");
    }

    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean isUpdateSupport) throws Exception {
        ExcelUtil<Category> util = new ExcelUtil<>(Category.class);
        List<Category> categoryList = util.importExcel(file.getInputStream());
        String operName = SecurityUtils.getUsername();
        String message = categoryService.importCategory(categoryList, isUpdateSupport, operName);
//        String message = "成功测试";
        return AjaxResult.success(message);
    }

    @InnerAuth
    @Operation(summary = "获取商品一级分类")
    @GetMapping(value = "/getOneCategory")
    public R<List<CategoryVo> > getOneCategory() {
        return R.ok(categoryService.getOneCategory());
    }

    @InnerAuth
    @Operation(summary = "获取分类嵌套列表")
    @GetMapping(value = "/tree")
    public R<List<CategoryVo> > tree() {
        return R.ok(categoryService.tree());
    }
}
